<template>
  <div class="box">
    <p class="title">编辑砍价活动</p>
    <el-form :model="info" status-icon ref="form" label-width="145px" :rules="rules">
        <el-input v-model="info.bargain_id" style="display:none"></el-input>
      <el-form-item label="产品id" prop="product_id">
        <el-input v-model="info.product_id"></el-input>
      </el-form-item>
      <el-form-item label="活动价格" prop="activity_money">
        <el-input v-model="info.activity_money"></el-input>
      </el-form-item>
      <el-form-item label="每次砍价最低价格" prop="bargain_min_price">
        <el-input v-model="info.bargain_min_price"></el-input>
      </el-form-item>
      <el-form-item label="每次砍价最高价格" prop="bargain_max_price">
        <el-input v-model="info.bargain_max_price"></el-input>
      </el-form-item>
      <el-form-item label="活动时间" prop="begin_time">
        <el-input v-model="info.begin_time" style="display:none"></el-input>
        <el-input v-model="info.end_time" style="display:none"></el-input>
        <el-date-picker
          v-model="time"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="timeChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="info.status" placeholder="请选择">
          <el-option label="禁用" :value="0"></el-option>
          <el-option label="启用" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="活动描述" prop="product_desc">
        <el-input type="textarea" v-model="info.product_desc"></el-input>
      </el-form-item>
      <el-form-item class="all-handle-btn-box">
        <el-button type="primary" @click="handleCertain(info)">确定</el-button>
        <router-link class="back-btn" to="/product/list"><el-button @click="$router.back()">返回</el-button></router-link>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      time: '',
      info: {},
      rules: {
        product_id: {required: true, message: '产品id不能为空', trigger: 'change'},
        product_desc: {required: true, message: '活动描述不能为空', trigger: 'change'},
        activity_money: {required: true, message: '活动价格不能为空', trigger: 'change'},
        status: {required: true, message: '状态不能为空', trigger: 'change'},
        bargain_min_price: {required: true, message: '砍价最低价格不能为空', trigger: 'change'},
        bargain_max_price: {required: true, message: '砍价最高价格不能为空', trigger: 'change'},
        begin_time: {required: true, message: '活动时间不能为空', trigger: 'blur'},
      }
    }
  },
  created() {
    let info = JSON.parse(window.sessionStorage.bargainInfo);
    this.info = info;
    this.time = [new Date(info.begin_time * 1000), new Date(info.end_time * 1000)];
  },
  methods: {
    handleCertain(info) {
      let _this = this;
      this.$refs.form.validate(bool => {
        if (bool) {
          this.$confirm("确定保存结果么?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            //添加操作
            this.$ajax({
              url: "bargain/setBargain",
              data: {
                app: info
              },
              success(res) {
                if (res.data.code === 1) {
                  _this.$message({
                    type: "success",
                    message: res.data.msg
                  });
                  _this.$router.push("/bargain/list");
                } else {
                  _this.$message({
                    type: "warning",
                    message: res.data.msg
                  });
                }
              }
            });
          }).catch(() => {});
        }
      });
    },
    timeChange(res) {
      if (!res) {
        return;
      }
      this.$set(this.$data.info, 'begin_time', parseInt(res[0].getTime() / 1000));
      this.$set(this.$data.info, 'end_time', parseInt(res[1].getTime() / 1000));
    }
  }
};
</script>

<style scoped>
.el-form {
  width: 50%;
  margin: 0 auto;
}
.title {
  padding: 20px 0;
  font-size: 20px;
  text-align: center;
}
.back-btn {
  margin-left: 10px;
}
</style>
